/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.Home;
import com.thinkgem.jeesite.modules.shop.service.HomeService;

import java.util.List;

/**
 * 收货地址Controller
 *
 * @author 马勇强
 * @version 2019-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/home")
public class HomeController extends BaseController {

    @Autowired
    private HomeService homeService;

    @ModelAttribute
    public Home get(@RequestParam(required = false) String id) {
        Home entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = homeService.get(id);
        }
        if (entity == null) {
            entity = new Home();
        }
        return entity;
    }

    @RequiresPermissions("shop:home:view")
    @RequestMapping(value = {"list", ""})
    public String list(Home home, HttpServletRequest request, HttpServletResponse response, Model model) {
        String rolename = UserUtils.getUser().getRoleNames();
        if (rolename.contains("普通用户")) {
            User user = UserUtils.getUser();
            home.setUser(user);
            Page<Home> page = homeService.findPage(new Page<Home>(request, response), home);
            model.addAttribute("page", page);
        } else {
            Page<Home> page = homeService.findPage(new Page<Home>(request, response), home);
            model.addAttribute("page", page);
        }
        return "modules/shop/homeList";
    }

    @RequiresPermissions("shop:home:view")
    @RequestMapping(value = "form")
    public String form(Home home, Model model) {
        model.addAttribute("home", home);
        return "modules/shop/homeForm";
    }

    @RequiresPermissions("shop:home:edit")
    @RequestMapping(value = "save")
    public String save(Home home, Model model, RedirectAttributes redirectAttributes) {
        User user = UserUtils.getUser();
        home.setUser(user);

        if (!beanValidator(model, home)) {
            return form(home, model);
        }

        homeService.save(home);
        addMessage(redirectAttributes, "保存收货地址成功");
        return "redirect:" + Global.getAdminPath() + "/shop/home/?repage";
    }

    @RequiresPermissions("shop:home:edit")
    @RequestMapping(value = "delete")
    public String delete(Home home, RedirectAttributes redirectAttributes) {
        homeService.delete(home);
        addMessage(redirectAttributes, "删除收货地址成功");
        return "redirect:" + Global.getAdminPath() + "/shop/home/?repage";
    }

}